package com.example.testapplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.testapplication.entity.Information;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class GsonActivity extends ApplicationActivity{
    private Handler handler; // 定义一个Android.os.Handle对象
    private String result = ""; // 定义一个代表显示内容的字符串
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_json);
        final TextView step = (TextView) findViewById(R.id.text1); // 显示单日步数
        final TextView time = (TextView) findViewById(R.id.text2);//获取TextView显示单日时间
        final TextView heat = (TextView) findViewById(R.id.text3);//获取TextView显示单日热量
        final TextView km = (TextView) findViewById(R.id.text4); //获取TextView显示单日公里数
        final TextView step1 = (TextView) findViewById(R.id.text5);//获取TextView显示周步数
        final TextView time1 = (TextView) findViewById(R.id.text6);//获取TextView显示周时间
        final TextView heat1 = (TextView) findViewById(R.id.text7);//获取TextView显示周热量
        final TextView km1 = (TextView) findViewById(R.id.text8); //获取TextView显示周公里数

        handler = new Handler(){
            @Override
            public void handleMessage(Message msg){
                // 创建TextView二维数组
                TextView[][] tv = {{step, time, heat, km}, {step1, time1, heat1, km1}};
                // 创建解析JSON的类型
                Type listType = new TypeToken<ArrayList<Information>>(){}.getType();
                // 创建解析JSON类型
                ArrayList<Information> foos = new Gson().fromJson(result,listType);
                for(int i = 0;i < foos.size();i++){
                    tv[i][0].setText(foos.get(i).getStep());     //获取JSON中的步数值
                    tv[i][1].setText(foos.get(i).getTime());     //获取JSON中的时间值
                    tv[i][2].setText(foos.get(i).getHeat());     //获取JSON中的热量值
                    tv[i][3].setText(foos.get(i).getKm());       //获取JSON中的公里数
                }
                super.handleMessage(msg);
            }
        };
        new Thread(new Runnable() {
            @Override
            public void run() {
                send();
                Message m = handler.obtainMessage();
                handler.sendMessage(m);
            }
        }).start();
    }

    public void send(){
        // 请求地址
        String target = "http://192.168.0.105:8080/example/index.json";
        // 创建OkHttpClient对象
        OkHttpClient okHttpClient = new OkHttpClient();
        // 创建网络请求
        Request request = new Request.Builder().get().url(target).build();

        try {
            // 创建请求响应
            Response response = okHttpClient.newCall(request).execute();
            result = response.body().string();// 获取json数据
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
